/**
 * 
 */
package TextEditor.facade;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.JTextArea;

/**
 * Implementation of FileInterface to paste selected text into a cursor position
 * @author Yij_zhan
 *
 */
public class PasteOperation implements FileInterface {

	Toolkit toolkit = Toolkit.getDefaultToolkit();
	Clipboard clipBoard = toolkit.getSystemClipboard();

	@Override
	public void fileOperation(JTextArea textArea) {
		textArea.requestFocusInWindow();
		Transferable contents = clipBoard.getContents(this);
		
		if(contents == null)  return;
		String pasteString = "";

		try {
			pasteString = (String)contents.getTransferData(DataFlavor.stringFlavor);
		} catch (UnsupportedFlavorException e1) {
			
			e1.printStackTrace();
		} catch (IOException e1) {
			
			e1.printStackTrace();
		}

		textArea.replaceRange(pasteString,textArea.getSelectionStart(),textArea.getSelectionEnd());

		
	}

	

}
